package com.cn.ant.modules.app.web;

import com.cn.ant.common.utils.*;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.pay.cmbc.entity.CmbcReq;
import com.cn.ant.modules.pay.cmbc.entity.LcbpPayReq;
import com.cn.ant.modules.pay.cmbc.utils.CMBCUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 项目名称：linghang
 * 类名称：
 * 类描述：
 *
 * @version 1.0
 * @author：huanggenhua
 * @date：2017/6/25 11:57
 */
@Controller
@RequestMapping(value = "${appPath}")
public class PayController extends BaseController {

    @RequestMapping(value = { "", "pay" })
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "app/pay";
    }

    /**
     * 微信正扫
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "wxzs")
    @ResponseBody
    public AjaxJson wxzs(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson result = new AjaxJson();
        String url = "http://wxpay.cmbc.com.cn/mobilePlatform/appserver/lcbpPay.do";
        //封装context内容
        LcbpPayReq req = new LcbpPayReq();
        req.setAmount(1);
        req.setMerchantNo("M01002016090000001273");
        req.setMerchantSeq(Identities.generateMainSn());
        req.setNotifyUrl("http://wxpay.cmbc.com.cn/cmbcpaydemo/NoticeServlet?name=notice");
        req.setOrderInfo("统一下单DEMO-API_WXQRCODE");
        req.setPlatformId("A00012017050000000545");
        req.setRemark("");
        req.setSelectTradeType("API_WXQRCODE");
        req.setTransDate(DateUtils.getDate("yyyyMMdd"));
        req.setTransTime(DateUtils.getCurrDateForLong());
        //1.获取签名
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String context = gson.toJson(req);
        String reqSign = CMBCUtils.getSign(context);

        //2.生成加密报文
        String signContext = CMBCUtils.sign(reqSign, context);

        //3.加密报文
        String encryptContext = CMBCUtils.encrypt(signContext);

        //4.生成调用报文
        CmbcReq cmbcReq = new CmbcReq();
        cmbcReq.setVersion("");
        cmbcReq.setTransTime("");
        cmbcReq.setTransDate("");
        cmbcReq.setTransCode("");
        cmbcReq.setSource("");
        cmbcReq.setSecurityType("");
        cmbcReq.setMerchantNo("");
        cmbcReq.setMerchantSeq("");
        cmbcReq.setReserveJson("");
        cmbcReq.setReserve1("");
        cmbcReq.setReserve2("");
        cmbcReq.setReserve3("");
        cmbcReq.setReserve4("");
        cmbcReq.setReserve5("");
        cmbcReq.setBusinessContext(encryptContext);
        //5.生成调用报文
        Map<String, String> params = new HashMap<>();
        MyBeanUtils.copyBean2Map(params, cmbcReq);
        String parameters = gson.toJson(cmbcReq);
        //parameters = "{\"businessContext\":\"MIIC+AYKKoEcz1UGAQQCA6CCAugwggLkAgECMYGdMIGaAgECgBS1x/e/puEJbLQnTtwm2y/+fP1b2jANBgkqgRzPVQGCLQMFAARw+uPslxAwK69yG3yOZSCEx0LWWroNPl+8hdRiO8XN9Y2i8WvYlBewfcJ3ql+w+B22q6H5S3AbT/WxTVzknrOZO5BETSmdHdHY4BZO763QKN1YDmdX3EWCJLDIxyd9EFJEhhTAB7n9fG2htBNJN7A3fzCCAj0GCiqBHM9VBgEEAgEwGwYHKoEcz1UBaAQQMuybktHZFnL8UqkGwSLApYCCAhBqV2HIPAjV36H2rchfU7DqqVQRqF+9hxW6kbS6mUBKGdSrzhK9Jl4k9si6TS6YPhgVabqGank8vXl3k9qQSiJxFkFciJk5XZ1Tdq6MP5PD7fNQkqYo9GgZpPagehjUKtm0lCoVNU+Vhct3IlX435aoCtGbxrizfSuidQcV8hbre7gVZmmBBYpBGLP2HXU0K3hxtHubNvRMhUViMsKjBdhMkWs75Sq5nJYddONW7BmBuVvW5yxfPI1WyAS6Mhr15THa7ZKWUsMFKOHOP3H2H0Uz3kka5qrep3VF+tcwN2JqvmEYp5l5srGC7SY5jqvrks10EazPpfGrTe/FClcD26djg+NjZjvyNDmFr0ZHjxj3Y5BoaSqWWNxD3Xt+6Bd/dCuzwcNQBdHrdzFi3wLtNeEgznTHDIguZDJt06LSkbwbGN29FaVypeX9Idycoao1fLaBQIS/NjRrW1HXtTLBTVsDWQszkAQo+JfeU1swge4j0mkyJbw5SkJOXnkS6FKi3YBNhDQay7gGQMPYEOeG9kaNnj+6JDbpqh9ISS9zj1zgOEiotv5j2efRDP5hQNB892fshVqXJiGoSKoZPexxIpxeQFljVxXx9+CMFx/j3EY28gOMAT+oO9eK9X1F8J+M9uE4DNek8VhYf2b8n3SLkrt2gp0O8fp3uRkx1VH+bj/T7uHl6N1AQloc9estuQP0viE=\",\"merchantNo\":\"\",\"merchantSeq\":\"\",\"reserve1\":\"\",\"reserve2\":\"\",\"reserve3\":\"\",\"reserve4\":\"\",\"reserve5\":\"\",\"reserveJson\":\"\",\"securityType\":\"\",\"sessionId\":\"\",\"source\":\"\",\"transCode\":\"\",\"transDate\":\"\",\"transTime\":\"\",\"version\":\"\"}";
        JSONObject jo = HttpClientUtils.postByJson(url, parameters);

        //6.获取返回的密文
        String businessContext = jo.getString("businessContext");
        //7.解密
        String dncryptContext = CMBCUtils.dncrypt(businessContext);
        //8.验签
        boolean isSign = CMBCUtils.signCheck(dncryptContext);
        JSONObject payJo = JSONObject.fromObject(dncryptContext);
        JSONObject bodyJo = JSONObject.fromObject(payJo.get("body"));
        result.setSuccess(true);
        result.setObj(bodyJo);
        return result;
    }

}
